<?php

namespace app\api\controller;
use app\Request;
use think\facade\Db;

class Common extends Api
{

    //天气接口（7天） UI参考
    public function weather(Request $request)
    {
        $province=$request->param("province");
        $city=$request->param("city");
        $county=$request->param("county");
        if (empty($province)){
            $this->error("省份信息错误！");
        }
        if (empty($city)){
            $this->error("城市信息错误！");
        }

        $url="https://wis.qq.com/weather/common?source=pc&weather_type=observe|forecast_1h|forecast_24h|index|alarm|limit|tips|rise&province=".$province."&city=".$city."&county=".$county;
        $return=$this->getRequest($url);
        $this->code($return["data"]["forecast_24h"]);
        if($return["data"]){
            $this->success($return["data"]);
        }else{
            $this->error("error");
        }
    }

    //空气接口
    public function air(Request $request)
    {
        $province=$request->param("province");
        $city=$request->param("city");
        $county=$request->param("county");
        if (empty($province)){
            $this->error("省份信息错误！");
        }
        if (empty($city)){
            $this->error("城市信息错误！");
        }
        $url="https://wis.qq.com/weather/common?source=pc&weather_type=air|rise&province=".$province."&city=".$city."&county=".$county;
        $return=$this->getRequest($url);
        if($return["data"]){
            $this->success($return["data"]);
        }else{
            $this->error("error");
        }
    }

    //14天 https://j.i8tq.com/weather2020/search/city.js（城市代码）
    public function weather14(Request $request)
    {
        $AREAID=$request->param("AREAID");
        $date=$request->param("date","2024-07-26");
        $time=[];
        for($i=1;$i<9;$i++){
            $date=date('Y-m-d',strtotime($date.'+'.$i.'day'));
            $time[]=$date;
        }
        $url="http://www.weather.com.cn/weather15d/".$AREAID.".shtml";
        $return=file_get_contents($url);
        $match_js_content = '/<ul class="t clearfix">(.+?)<\/ul>/is';
        preg_match_all($match_js_content, $return, $return_content);
        $return=$return_content[1][0];
        $return=str_replace("</em>","",$return);
        $return=str_replace("℃","",$return);
        $return=str_replace("级","",$return);


        $match_js_content = '/<span class="wea">(.+?)<\/span>/is';
        preg_match_all($match_js_content, $return, $wea);

        $match_js_content = '/<span class="wind">(.+?)<\/span>/is';
        preg_match_all($match_js_content, $return, $wind);

        $match_js_content = '/<span class="wind1">(.+?)<\/span>/is';
        preg_match_all($match_js_content, $return, $wind1);

        $match_js_content = '/<span class="tem"><em>(.+?)<\/span>/is';
        preg_match_all($match_js_content, $return, $tem);



//        $match_js_content = '/<big class="png30 (.+?)"></is';
//        preg_match_all($match_js_content, $return, $big);

        $names=DB::table("zl_weather")->select()->toArray();
        $name_array=[];
        foreach ($names as $name)
        {
            $name_array[$name["name"]]=$name["code"];
        }

        $return=[];
        foreach ($time as $dk=>$da)
        {
            $datail=[
                'aqi_level'=>0,
                'aqi_name'=>"",
                'aqi_url'=>"",
                'time'=>$da,
            ];
            //天气
            for ($i=0;$i<=7;$i++)
            {
                if($dk == $i)
                {
                    $weaValue=$wea[1][$i];
                    if(strpos($weaValue,"转"))
                    {
                        $weaValue1=explode("转",$weaValue);
                        $datail["day_weather"]=$weaValue1[0];
                        $datail["day_weather_code"]=isset($name_array[$weaValue1[0]]) ? $name_array[$weaValue1[0]] :"";
                        $datail["day_weather_short"]=$weaValue1[0];
                        $datail["day_weather_url"]="";

                        $datail["night_weather"]=$weaValue1[1];
                        $datail["night_weather_code"]=isset($name_array[$weaValue1[1]]) ? $name_array[$weaValue1[1]] :"";
                        $datail["night_weather_short"]=$weaValue1[1];
                        $datail["night_weather_url"]="";
                    }else{
                        $datail["day_weather"]=$weaValue;
                        $datail["day_weather_code"]=isset($name_array[$weaValue]) ? $name_array[$weaValue] :"";
                        $datail["day_weather_short"]=$weaValue;
                        $datail["day_weather_url"]="";

                        $datail["night_weather"]=$weaValue;
                        $datail["night_weather_code"]=isset($name_array[$weaValue]) ? $name_array[$weaValue] :"";
                        $datail["night_weather_short"]=$weaValue;
                        $datail["night_weather_url"]="";
                    }
                }

            }


            //温度
            for ($i=0;$i<=7;$i++)
            {
                if($dk == $i)
                {
                    $temValue=$tem[1][$i];
                    $temValue1=explode("/",$temValue);
                    $datail["min_degree"]=$temValue1[1];
                    $datail["max_degree"]=$temValue1[0];
                }

            }

            //风向
            for ($i=0;$i<=7;$i++)
            {
                if($dk == $i)
                {
                    $windValue=$wind[1][$i];
                    if(strpos($windValue,"转"))
                    {
                        $windValue1=explode("转",$windValue);
                        $datail["day_wind_direction"]=$windValue1[0];
                        $datail["day_wind_direction_code"]="";

                        $datail["night_wind_direction"]=$windValue1[1];
                        $datail["night_wind_direction_code"]="";
                    }else{
                        $datail["day_wind_direction"]=$windValue;
                        $datail["day_wind_direction_code"]="";

                        $datail["night_wind_direction"]=$windValue;
                        $datail["night_wind_direction_code"]="";
                    }
                }

            }


            //风力
            for ($i=0;$i<=7;$i++)
            {
                if($dk == $i)
                {
                    $wind1Value=$wind1[1][$i];
                    if(strpos($wind1Value,"转"))
                    {
                        $windValue1=explode("转",$wind1Value);
                        $datail["day_wind_power"]=$windValue1[0];
                        $datail["day_wind_power_code"]="";

                        $datail["night_wind_power"]=$windValue1[1];
                        $datail["night_wind_power_code"]="";
                    }else{
                        $datail["day_wind_power"]=$wind1Value;
                        $datail["day_wind_power_code"]="";

                        $datail["night_wind_power"]=$wind1Value;
                        $datail["night_wind_power_code"]="";
                    }
                }

            }


            $return[]=$datail;

        }
        if($return){
            $this->success($return);
        }else{
            $this->error("error");
        }
    }

    //code 映射保存
    public function code($data)
    {
        $names=DB::table("zl_weather")->field("name")->select()->toArray();
        $names=array_column($names,"name");
        $stock=[];
        foreach ($data as $d)
        {
            if(!in_array($d["day_weather"],$names))
            {
                $stock[]=[
                    "name"=>$d["day_weather"],
                    "code"=>$d["day_weather_code"]
                ];
            }
            if(!in_array($d["night_weather"],$names))
            {
                $stock[]=[
                    "name"=>$d["night_weather"],
                    "code"=>$d["night_weather_code"]
                ];
            }
        }

        if($stock) DB::table("zl_weather")->insertAll($stock);
    }

    //万年历 https://mobile.51wnl-cq.com/huangli_tab_h5/?posId=BDSS&STIME=2024-07-22

    //节日分类
    public function festivalCate()
    {
        $list=[
            ["name"=>"传统节日","child"=>[
                ["id"=>35,"name"=>"腊八"],
                ["id"=>36,"name"=>"小年"],
                ["id"=>37,"name"=>"除夕"],
                ["id"=>38,"name"=>"春节"],
                ["id"=>39,"name"=>"元宵节"],
                ["id"=>40,"name"=>"龙抬头"],
                ["id"=>41,"name"=>"寒食节"],
                ["id"=>42,"name"=>"端午节"],
                ["id"=>43,"name"=>"七夕节"],
                ["id"=>44,"name"=>"中秋节"],
                ["id"=>45,"name"=>"重阳节"]
            ]],
            ["name"=>"日常节日","child"=>[
                ["id"=>46,"name"=>"元旦节"],
                ["id"=>47,"name"=>"214情人节"],
                ["id"=>48,"name"=>"妇女节"],
                ["id"=>49,"name"=>"白色情人节"],
                ["id"=>50,"name"=>"愚人节"],
                ["id"=>51,"name"=>"劳动节"],
                ["id"=>52,"name"=>"青年节"],
                ["id"=>53,"name"=>"母亲节"],
                ["id"=>54,"name"=>"护士节"],
                ["id"=>55,"name"=>"520"],
                ["id"=>56,"name"=>"儿童节"],
                ["id"=>57,"name"=>"父亲节"],
                ["id"=>58,"name"=>"教师节"],
                ["id"=>59,"name"=>"国庆节"],
                ["id"=>60,"name"=>"感恩节"],
                ["id"=>61,"name"=>"平安夜"],
                ["id"=>62,"name"=>"圣诞节"],
                ["id"=>63,"name"=>"高考加油"]
            ]],
            ["name"=>"二十四节气","child"=>[
                ["id"=>11,"name"=>"立春"],
                ["id"=>12,"name"=>"雨水"],
                ["id"=>13,"name"=>"惊蛰"],
                ["id"=>14,"name"=>"春分"],
                ["id"=>15,"name"=>"清明"],
                ["id"=>16,"name"=>"谷雨"],
                ["id"=>17,"name"=>"立夏"],
                ["id"=>18,"name"=>"小满"],
                ["id"=>19,"name"=>"芒种"],
                ["id"=>20,"name"=>"夏至"],
                ["id"=>21,"name"=>"小暑"],
                ["id"=>22,"name"=>"大暑"],
                ["id"=>23,"name"=>"立秋"],
                ["id"=>24,"name"=>"处暑"],
                ["id"=>25,"name"=>"白霜"],
                ["id"=>26,"name"=>"秋分"],
                ["id"=>27,"name"=>"寒露"],
                ["id"=>28,"name"=>"霜降"],
                ["id"=>29,"name"=>"立冬"],
                ["id"=>30,"name"=>"小雪"],
                ["id"=>31,"name"=>"大雪"],
                ["id"=>32,"name"=>"冬至"],
                ["id"=>33,"name"=>"小寒"],
                ["id"=>34,"name"=>"大寒"]
            ]],
            ["name"=>"其他","child"=>[
                ["id"=>94,"name"=>"早上"],
                ["id"=>95,"name"=>"上午"],
                ["id"=>96,"name"=>"中午"],
                ["id"=>97,"name"=>"下午"],
                ["id"=>98,"name"=>"晚上"],
                ["id"=>63,"name"=>"周一"],
                ["id"=>64,"name"=>"周二"],
                ["id"=>65,"name"=>"周三"],
                ["id"=>66,"name"=>"周四"],
                ["id"=>67,"name"=>"周五"],
                ["id"=>68,"name"=>"周六"],
                ["id"=>69,"name"=>"周日"]
            ]]
        ];

        $this->success($list);


        //早上 文章  95上午 96中午 97下午 98晚上  63周一 69
        //https://meiri.58xianhua.com/wishes/list?page=1&pageSize=20&attrs=[94]
        //https://meiri.58xianhua.com/wishes/view?id=827
        //https://meiri.58xianhua.com/images/random?pageSize=1&tagid=94

        //早上 图片 https://meiri.58xianhua.com/images/attrimg?page=1&pageSize=20&attrs=%5B94%5D&orderby=



        //早上好多图片 https://image.baidu.com/search/acjson?tn=resultjson_com&pn=24&rn=12&word=%E6%97%A9%E4%B8%8A%E5%A5%BD%20%E6%97%A9%E5%AE%89%20%E5%8A%A8%E5%9B%BE%20%E7%BE%8E%E5%A5%BD


        //https://meiri.58xianhua.com/festivals/today 最近的节日


        //祝福美文 https://meiri.58xianhua.com/articles/index?page=1&pageSize=20&cate=0&attrs=[]&orderby=
        //详情 https://meiri.58xianhua.com/articles/view?id=142
    }


    //节日今日
    public function festivalToday()
    {
        $url="https://meiri.58xianhua.com/festivals/today";
        $data=$this->getRequest($url);
        unset($data["config"]);
        unset($data["banner"]);
        $this->success($data);
    }

    //节日 文字
    public function festivalContent(Request $request)
    {
        $id=$request->param("id",35);
        $page=$request->param("page",1);
        $url="https://meiri.58xianhua.com/wishes/list?page=1&pageSize=".$page."&attrs=[".$id."]";
        $data=$this->getRequest($url);
        $this->success($data);
    }

    //节日 文字详情
    public function festivalContentDetail(Request $request)
    {
        $id=$request->param("id",12441);
        $url="https://meiri.58xianhua.com/wishes/view?id=".$id;
        $data=$this->getRequest($url);
        $this->success($data);
    }

    //节日 文字详情图片
    public function festivalContentImage(Request $request)
    {
        $id=$request->param("id",35);
        $url="https://meiri.58xianhua.com/images/random?pageSize=1&tagid=".$id;
        $data=$this->getRequest($url);
        $data[0]["img"]="https://meiri.58xianhua.com".$data[0]["img"];
        $this->success($data);
    }


    //节日 图片
    public function festivalImage(Request $request)
    {
        $id=$request->param("id",35);
        $page=$request->param("page",1);
        $url="https://meiri.58xianhua.com/images/attrimg?page=".$page."&pageSize=20&attrs=[".$id."]";
        $data=$this->getRequest($url);
        foreach ($data as &$v)
        {
            $v["img"]="https://meiri.58xianhua.com".$v["img"];
            unset($v["alarelation"]);
        }
        $this->success($data);
    }

    //节日 文字详情图片
    public function festivalImageContent(Request $request)
    {
        $id=$request->param("id",35);
        $url="https://jieri.58xianhua.com/wishes/random?attr=".$id."&pageSize=1";
        $data=$this->getRequest($url);
        unset($data[0]["warelation"]);
        $this->success($data);
    }



    //节日 文章
    public function festivalArticle(Request $request)
    {
        $page=$request->param("page",1);
        $url="https://meiri.58xianhua.com/articles/index?page=".$page."&pageSize=20&cate=0&attrs=[]&orderby=";
        $data=$this->getRequest($url);
        $this->success($data);
    }


    //节日 文章
    public function festivalArticleDetail(Request $request)
    {
        $id=$request->param("id",142);
        $url="https://meiri.58xianhua.com/articles/view?id=".$id;
        $data=$this->getRequest($url);
        $this->success($data);
    }








}